package com.three.aboutus.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.aboutus.service.AboutService;
import com.three.aboutus.utils.Upload;
import com.three.core.entity.About;
import com.three.core.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @FileName: AboutController
 * @Description:
 * @Author: GYX
 * @CreateTime: 2025/3/14 15:33
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/aboutus")
public class AboutController {

    @Autowired
    private AboutService aboutService;

    @PostMapping("/selectAboutUs")
    public R<Page<About>> selectAboutUs(Integer currentPage, Integer pageSize){
        return aboutService.findAboutByCondition(currentPage,pageSize);
    }

    @GetMapping("/deleteAboutUsById")
    public R deleteAboutUsById(Integer id){
        return aboutService.deleteAboutById(id);
    }

    @PostMapping("/insertAboutUs")
    public R<About> insertAboutUs(@RequestBody About about){
        return aboutService.insertAbout(about);
    }

    @PostMapping("/updateAboutUsById")
    public R updateAboutUsById(@RequestBody About about){
        return aboutService.updateAbout(about);
    }

    @PostMapping ("/updateIsvalidById")
    public R<About> updateIsvalidById(@RequestBody About about){
        return aboutService.updateIsValid(about);
    }

    @PostMapping("/selectByName")
    public R<About> selectByName(@RequestParam String name){
        return aboutService.selectByName(name);
    }

    @PostMapping("/uploadFileEditor")
    public Dict uploadFile(@RequestParam("file") MultipartFile myfile, @RequestParam String type) throws IOException {
        String url = Upload.uploadOss(myfile);
        if("img".equals(type)){
            return Dict.create().set("errno",0).set("data", CollUtil.newArrayList(Dict.create().set("url",url)));
        }else if("video".equals(type)){
            return Dict.create().set("errno" ,0).set("data", Dict.create().set("url",url));
        }
        return Dict.create().set("errno",0);
    }

    @PostMapping("/selectFirst")
    public R<About> selectFirst(){
        return aboutService.selectFirst();
    }

}
